<?php

use yii\helpers\Html;
use common\components\GridView;
use yii\widgets\Pjax;
use yii\helpers\Url;
use kartik\editable\Editable;

/* @var $model common\modelsgii\Config */
/* @var $dataProvider yii\data\ActiveDataProvider  */
/* @var $searchModel backend\models\search\ConfigSearch */

$columns = [
    [
        'class' => \common\core\CheckboxColumn::className(),
        'name'  => 'id',
        'options' => ['width' => '20px;'],
        'checkboxOptions' => function ($model, $key, $index, $column) {
            return ['value' => $key,'label'=>'<span></span>','labelOptions'=>['class' =>'mt-checkbox mt-checkbox-outline','style'=>'padding-left:19px;']];
        }
    ],
    [
        'header' => 'ID',
        'attribute' => 'id',
        'options' => ['width' => '50px;']
    ],
    [
        'header' => '配置名称',
        'options' => ['width' => '200px;'],
        'attribute' => 'name'
    ],
    [
        'header' => '名称',
        'attribute' => 'title'
    ],
    [
        'header' => '类型',
        'options' => ['width' => '50px;'],
        'content' => function($model, $key){
            switch ($model['type']){
                case 0 :
                    return '数字'; 
                case 1 :
                    return '字符';
                case 2 :
                    return '文本';
                case 3 :
                    return '数组';
                case 4 :
                    return '枚举';
            }
        }
    ],
    [
        'label' => '分组',
        'options' => ['width' => '50px;'],
        'value' => function($model){
            switch ($model['group']){
                case 0 :
                    return ''; 
                case 1 :
                    return '基本';
                case 2 :
                    return '内容';
                case 3 :
                    return '用户';
                case 4 :
                    return '系统';
            }
        }
    ],
    [
        'label' => '排序',
        'value' => 'sort',
        'options' => ['width' => '50px;'],
    ],
    [
        'label' => '状态',
        'options' => ['width' => '50px;'],
        'content' => function($model){
            if ($model['status']) {
                return '正常';
            }
            return '<span style="color:#f00;">隐藏</span>';
        }
    ],
    [
        'class' => 'yii\grid\ActionColumn',
        'header' => '操作',
        'template' => '{update} {delete}',
        'options' => ['width' => '200px;'],
        'buttons' => [
            'update' => function ($url, $model, $key) {
                return Html::a('<i class="fa fa-edit"></i> 更新', ['update','id'=>$key], [
                    'title' => Yii::t('app', '更新'),
                    'class' => 'btn btn-xs green'
                ]);
            },
            'delete'=>function($url,$model,$key)
            {
                $options=[
                    'title'=>Yii::t('app', '删除'),
                    'aria-label'=>Yii::t('app','删除'),
                    'data-confirm'=>Yii::t('app','您确定要执行删除操作吗？'),
                    'data-method'=>'post',
                    'data-pjax'=>'0',
                    'class' => 'btn btn-xs red'
                ];
                return Html::a('<i class="fa fa-times"></i> 删除',$url,$options);

            }
        ],
    ],
];

?>

<?php Pjax::begin(['id'=>'grid-pjax']); ?>
<div class="premise-tag-index">

    <?php  echo $this->render('_search', ['model' => $searchModel]); ?>

    <?= GridView::widget([
        'id' => 'grid-id',
        'dataProvider' => $dataProvider,
//        'filterModel' => $searchModel,
//        'showPageSummary' => true,
        'pjax' => true,
        'striped' => true,
        'hover' => true,
//        'export' => false,
        'toolbar' => [
            '{export}',
            ['content' =>
                Html::a('<i class ="glyphicon glyphicon-repeat"></i>', ['index'], ['data-pjax' => 0, 'class' => 'btn btn-default', 'title' => 'Reset Grid'])
            ],
            'options' => ['class' => 'btn-group-sm']
        ],
        'exportConfig' => [
            GridView::CSV => ['label' => 'CSV','icon' => 'floppy-open' ],
        ],
        'panel' => [
            'heading' => '<i class="glyphicon glyphicon-th-list"></i>',
            'type' => 'default',
            'before' => Html::a('<i class="glyphicon glyphicon-plus"></i> 添加', ['create'], ['class' => 'btn btn-primary btn-sm']) . ' ' .
                Html::a('<i class="glyphicon glyphicon-edit"></i> 编辑', 'javascript:void(0);', ['class' => 'btn btn-success btn-sm btn-update']) . ' ' .
                Html::a('<i class="glyphicon glyphicon-remove"></i> 删除', 'javascript:void(0);', ['class' => 'btn btn-danger btn-sm batch-delete']),
        ],
        'pager' => [
            'firstPageLabel' => '首页',
            'lastPageLabel' => '尾页',
            'registerLinkTags' => true,
            'disableCurrentPageButton' => true,
            'searchPage' => true,
            'searchPageLabel' => '跳至',
            'pageCount' => true,
            'pageSize' => true,
        ],
        'columns' => $columns,
    ]); ?>
</div>

<script language="JavaScript">
    <?php $this->beginBlock(Yii::$app->controller->route) ?>

    $(document).ready(function () {

        // 编辑
        $(".btn-update").on("click", function () {
            var ids = $("#grid-id").yiiGridView("getSelectedRows");
            if (!ids.length) {
                layer.alert('请选中一条记录', {'title': '提示', 'icon': 7});
                return false;
            }

            window.location.href = '<?= Url::toRoute('update') ?>' + '?id=' + ids[0];
        });

        // 批量删除
        $(".batch-delete").on("click", function () {
            var ids = $("#grid-id").yiiGridView("getSelectedRows");
            if (!ids.length) {
                layer.alert('请选择至少一条记录', {'title': '提示', 'icon': 7});
                return false;
            }

            // 询问框
            layer.confirm('您确定要执行删除操作吗？', {
                btn: ['确认', '取消'] //按钮
            }, function (index) {
                // 表单提交
                $.ajax({
                    url: '<?= Url::toRoute('delete') ?>' + '?sync=true&id=' + ids,
                    type: 'post',
                    dataType: "json",
                    data: {
                        _csrf: "<?= Yii::$app->request->csrfToken ?>"
                    },
                    beforeSend: function () {
                        layer.msg('删除中...');
                    },
                    success: function (r) {
                        layer.close(layer.index);
                        if (r.status == 1) {
                            $.pjax.reload({container: "#grid-pjax"});
                        } else {
                            layer.alert(r.info, {'title': '提示', 'icon': 7});
                        }
                    },
                    error: function () {
                        alert('系统错误');
                        return false;
                    }
                });
            }, function (index) {

            });


        });

    });

    jQuery(document).ready(function() {
        highlight_subnav('config/index'); //子导航高亮
    });

    <?php
    $this->endBlock();
    $this->registerJs($this->blocks[Yii::$app->controller->route], \yii\web\View::POS_END);
    ?>
</script>
<?php Pjax::end(); ?>
